import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_my_web/event/change_root_event.dart';
import 'package:flutter_my_web/logic/HomeGetLogic.dart';
import 'package:flutter_my_web/pages/home/HomeTopBarWidget.dart';
import 'package:flutter_my_web/pages/home/HomeTopWidget.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_my_web/widget/EmptyWidget.dart';
import 'package:get/get.dart';
import 'package:flutter_lib/utils/DpUtils.dart';

import 'HomeListItemView.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late ScrollController _ScrollController;
  final HomeGetLogic _homeGetLogic = Get.put(HomeGetLogic());
  @override
  void initState() {
    super.initState();
    _homeGetLogic.getHomeData();
    refreshHomeEventBus.on().listen((event) {
      print("-------------------");
      _homeGetLogic.getHomeData();
    });
  }

  @override
  Widget build(BuildContext context) {
    _ScrollController = _homeGetLogic.mScrollController;
    _ScrollController.addListener(() {
      _homeGetLogic.homeSingleViewOffset.value = _ScrollController.offset;
    });
    return GetBuilder<HomeGetLogic>(
      assignId: true,
      builder: (logic) {
        return Container(
          decoration: BoxDecoration(
              image: DecorationImage(
                  image:
                      NetworkImage(_homeGetLogic.jsonHomeDataResponse.bg ?? ""),
                  fit: BoxFit.cover)),
          child: Stack(
            children: [
              CustomScrollView(
                slivers: [
                  SliverToBoxAdapter(
                    child: HomeTopWidget(),
                  ),
                  SliverToBoxAdapter(
                    child: Container(
                      height: 50.0.wDp,
                    ),
                  ),
                  (_homeGetLogic.jsonHomeDataResponse.body == null ||
                          _homeGetLogic.jsonHomeDataResponse.body!.isEmpty)
                      ? SliverToBoxAdapter(
                          child: EmptyWidget(),
                        )
                      : SliverList(
                          delegate: SliverChildBuilderDelegate(
                          (BuildContext context, int index) {
                            return HomeListItemView(_homeGetLogic.bodyList[index]);
                          },
                          childCount:_homeGetLogic.bodyList.length,
                        )),
                  SliverToBoxAdapter(
                    child: Container(
                      height: 50.0.wDp,
                    ),
                  ),
                ],
                controller: _ScrollController,
              ),
              GestureDetector(
                  onLongPress: () {
                    refreshHome();
                  },
                  child: HomeTopBarWidget()),
            ],
          ),
        );
      },
    );
  }
}
